package top.cyuw.subjectsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.cyuw.subjectsystem.constant.SelectionState;
import top.cyuw.subjectsystem.entity.Selection;
import top.cyuw.subjectsystem.entity.User;
import top.cyuw.subjectsystem.exception.ServiceException;
import top.cyuw.subjectsystem.mapper.SelectionMapper;
import top.cyuw.subjectsystem.service.SelectionService;
import top.cyuw.subjectsystem.service.SubjectService;
import top.cyuw.subjectsystem.service.UserService;
import top.cyuw.subjectsystem.util.JwtTokenUtils;
import top.cyuw.subjectsystem.util.MapperUtils;
import top.cyuw.subjectsystem.vo.selection.SelectionVo;
import top.cyuw.subjectsystem.vo.subject.SubjectVo;

import java.util.Date;

@Service
public class SelectionServiceImpl extends ServiceImpl<SelectionMapper, Selection> implements SelectionService {

    @Autowired
    SubjectService subjectService;

    @Autowired
    UserService userService;

    @Override
    public Boolean finish() {
        Integer userId = JwtTokenUtils.getUserId();
        LambdaUpdateWrapper<Selection> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Selection::getUserId, userId);
        wrapper.set(Selection::getState, SelectionState.FINISHED);
        return super.update(wrapper);
    }

    @Override
    public SelectionVo select(Integer subjectId) {
        SubjectVo subjectVo = subjectService.getById(subjectId);
        if (subjectVo == null) {
            throw new ServiceException(400, "课题不存在！");
        }
        User user = userService.getById(JwtTokenUtils.getUserId());
        if (user == null || (user.getDepartId() != null && user.getDepartId() > 0 && !subjectVo.getLimits().contains(String.valueOf(user.getDepartId())))) {
            throw new ServiceException(400, "无法选择此课题！");
        }
        Selection selection = getMySelection();
        selection.setSubjectId(subjectId);
        selection.setState(SelectionState.SELECTED);
        super.updateById(selection);
        return MapperUtils.map(selection, SelectionVo.class);
    }

    private Selection getMySelection() {
        Integer userId = JwtTokenUtils.getUserId();
        LambdaQueryWrapper<Selection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Selection::getUserId, userId);
        Selection selection = super.getOne(wrapper, false);
        if (selection == null) {
            selection = new Selection();
            selection.setUserId(userId);
            selection.setSubjectId(0);
            selection.setState(0);
            Date now = new Date();
            selection.setCreateTime(now);
            selection.setUpdateTime(now);
            super.save(selection);
        }
        return selection;
    }

    @Override
    public SelectionVo my() {
        Selection selection = getMySelection();
        return MapperUtils.map(selection, SelectionVo.class);
    }

}
